// 页面路径：store/index.js 
import Vue from 'vue'
import Vuex from 'vuex'
import { reqLoginUser } from "@/api/login.js";

Vue.use(Vuex);//vue的插件机制

//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state: {
		// 账户列表
		accountListData: [],
		// 用户信息
		userInfo: {
			nickname: '',
			avatar: ''
		},
		// 订单详情
		orderDetail: {},
		newKlineTimer: null,
		KlineTimer: null
	},
	mutations: {
		// 设置账户列表
		setAccountListData(state, accountListData) {
			state.accountListData = accountListData
		},
		// 登录异步存储用户信息
		setUserInfoSync(state, userInfo) {
			state.userInfo = userInfo
			uni.setStorageSync('userInfo', userInfo);
		},
		// 设置订单详情
		setorderDetail(state, orderDetail) {
			state.orderDetail = orderDetail
		},
		setnewKlineTimer(state, newKlineTimer) {
			state.newKlineTimer = newKlineTimer
		},
		setKlineTimer(state, KlineTimer) {
			state.KlineTimer = KlineTimer
		}
	},
	actions: {
		// 注册异步存储用户信息
		setUserInfo({ state, commit }, sendData) {
			return new Promise((resolve, reject) => {
				reqLoginUser(sendData).then((res) => {
					console.log(res);
					uni.removeStorageSync('userInfo');
					commit('setUserInfoSync', res.data);
					setTimeout(() => {
						uni.navigateTo({
							url: "/pages/accountList/index",
						});
					}, 200);
					resolve();
				}).catch(error => {
					reject(error)
				})
			})
		}
	}
})
export default store
